package com.song.Inet.Socket1;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Author ZhangGuisong
 * @Date 2021/5/4-10:14
 */
public class SocketClinet {
    public static void main(String[] args) throws IOException {
        //创建客户端的Socket
        Socket socketClinet=new Socket(InetAddress.getLocalHost(),1234);
        //输出流
        OutputStream outputStream = socketClinet.getOutputStream();
        //将字符输出流转换成字节流
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream));
//        outputStream.write(new String("this is socketClient").getBytes());
        bufferedWriter.write("hello this is client");
        bufferedWriter.flush();
        socketClinet.shutdownOutput();
        //接收客户端发来的回应
        InputStream inputStream = socketClinet.getInputStream();
        //将字符输入流转换成字节流
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
//        byte[] bytes=new byte[1024];
//        int len=0;
//        while((len=inputStream.read(bytes))!=-1){
//            System.out.println(new String(bytes,0,len));
//        }
        String s = bufferedReader.readLine();
        System.out.println(s);
        bufferedReader.close();
        inputStream.close();
        bufferedWriter.close();
        //outputStream.flush();
        outputStream.close();
        socketClinet.close();
    }

}
